package com.bill.learn.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Bill Ludwig; 2020/5/14 18:13
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("user");

        if (user == null) {
            request.setAttribute("msg", "请先登录");
            request.getRequestDispatcher("/index").forward(request, response);
            return false;
        } else return true;
    }
}
